<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Introduction</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="kvaser.gif"/></td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('page_user_guide_intro.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">Introduction </div>  </div>
</div><!--header-->
<div class="contents">
<div class="toc"><h3>Table of Contents</h3>
<ul><li class="level1"><a href="#section_user_guide_intro_hello">Hello, CAN!</a></li>
<li class="level1"><a href="#section_user_guide_canstatus">Error checking</a></li>
<li class="level1"><a href="#section_core_api_calls">CANlib Core API Calls</a></li>
</ul>
</div>
<div class="textblock"><h1><a class="anchor" id="section_user_guide_intro_hello"></a>
Hello, CAN!</h1>
<p>Let's start with a simple example.</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="canlib_8h.html">canlib.h</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;stdio.h&gt;</span></div><div class="line"></div><div class="line"><span class="keywordtype">void</span> main(<span class="keywordtype">void</span>)</div><div class="line">{</div><div class="line">  <a class="code" href="canlib_8h.html#ae3d1b041d62207d5336f93c089cd5b65">canHandle</a> hnd;</div><div class="line"></div><div class="line">  <a class="code" href="group__can__general.html#gaff1ec1d3416d3bdd56336a7b9ac008b1">canInitializeLibrary</a>();</div><div class="line">  hnd = <a class="code" href="group___c_a_n.html#gac377d182232fb4ec2fed881c2b9ab300">canOpenChannel</a>(0, <a class="code" href="canlib_8h.html#afacd9e186d273d0dc24956ea0d433822">canOPEN_EXCLUSIVE</a>);</div><div class="line">  <span class="keywordflow">if</span> (hnd &lt; 0) {</div><div class="line">    <span class="keywordtype">char</span> msg[64];</div><div class="line">    <a class="code" href="group__can__general.html#ga01a7a415c95c579750bcdd95a1d245c4">canGetErrorText</a>((<a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7">canStatus</a>)hnd, msg, <span class="keyword">sizeof</span>(msg));</div><div class="line">    fprintf(stderr, <span class="stringliteral">&quot;canOpenChannel failed (%s)\n&quot;</span>, msg);</div><div class="line">    exit(1);</div><div class="line">  }</div><div class="line"></div><div class="line">  <a class="code" href="group___c_a_n.html#ga7eb8c2e92cfae57e7ec5031818524301">canSetBusParams</a>(hnd, <a class="code" href="canlib_8h.html#a87f618db13ec4f48610def72cfb40969">canBITRATE_250K</a>, 0, 0, 0, 0, 0);</div><div class="line">  <a class="code" href="group___c_a_n.html#gafca31590718ac7101d065b0c18b2410e">canSetBusOutputControl</a>(hnd, <a class="code" href="canlib_8h.html#ae5d980394050f52fd4af8f8ca75a4bca">canDRIVER_NORMAL</a>);</div><div class="line">  <a class="code" href="group___c_a_n.html#ga99c7c99cc71580f8099a1407f4f9ea1a">canBusOn</a>(hnd);</div><div class="line">  <a class="code" href="group___c_a_n.html#ga62c185329d6741c90102511e2f37983e">canWrite</a>(hnd, 123, <span class="stringliteral">&quot;HELLO!&quot;</span>, 6, 0);</div><div class="line">  <a class="code" href="group___c_a_n.html#ga304cb3a7bc2874c1f8ad361a911bcd5f">canWriteSync</a>(hnd, 500);</div><div class="line">  <a class="code" href="group___c_a_n.html#gaf1786cfbfd542b18b9c599d278837bd9">canBusOff</a>(hnd);</div><div class="line">  <a class="code" href="group___c_a_n.html#ga49525373a4d08d93c651ec10f79dd36b">canClose</a>(hnd);</div><div class="line">}</div></div><!-- fragment --><p><b>What does it do?</b></p><ol type="1">
<li>The CANlib library is initialized by a call to <a class="el" href="group__can__general.html#gaff1ec1d3416d3bdd56336a7b9ac008b1">canInitializeLibrary()</a>.</li>
<li>A channel to a CAN circuit is opened. In this case we open channel 0 which should be the first channel on the CAN interface. <a class="el" href="canlib_8h.html#afacd9e186d273d0dc24956ea0d433822">canOPEN_EXCLUSIVE</a> means we don't want to share this channel with any other currently executing program.</li>
<li>The CAN bus bit rate is set 250 kBit/s, using a set of predefined bus parameters.</li>
<li>The CAN bus driver type is set.</li>
<li>The CAN chip is activated.</li>
<li>A message with (11-bit) CAN id = 123, length 6 and contents (decimal) 72, 69, 76, 76, 79, 33 is transmitted.</li>
<li>Wait until the message is sent or at most 500 ms.</li>
<li>Inactivate the CAN chip.</li>
<li>Close the channel.</li>
</ol>
<p><b>What does it not do?</b> Almost all error checking is omitted for brevity, see <a class="el" href="page_user_guide_intro.html#section_user_guide_canstatus">Error checking</a>.</p>
<h1><a class="anchor" id="section_user_guide_canstatus"></a>
Error checking</h1>
<p>The CANlib API consists exclusively of functions. Most of the functions return a status code of type <a class="el" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7">canStatus</a>, which is a negative number if the call failed, see <a class="el" href="canstat_8h.html#canERR_xxx">canERR_xxx</a>, and <a class="el" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7a49743d0d438957118b9c6af2e831b209">canOK</a> (zero) if the call succeeded.</p>
<p>To get a textual description of an error code, use <a class="el" href="group__can__general.html#ga01a7a415c95c579750bcdd95a1d245c4">canGetErrorText()</a></p>
<p><b>Example</b>. Error checking of <a class="el" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7">canStatus</a> and using <a class="el" href="group__can__general.html#ga01a7a415c95c579750bcdd95a1d245c4">canGetErrorText()</a> to print a textual message.</p>
<div class="fragment"><div class="line"><span class="comment">// Print status message if stat contains an error code</span></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> checkStatus(<span class="keywordtype">char</span>* <span class="keywordtype">id</span>, <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7">canStatus</a> stat)</div><div class="line">{</div><div class="line">  <span class="keywordflow">if</span> (stat != canOK) {</div><div class="line">    <span class="keywordtype">char</span> buf[100];</div><div class="line">    buf[0] = <span class="charliteral">&#39;\0&#39;</span>;</div><div class="line">    <span class="comment">// Retreive informational text about the status code</span></div><div class="line">    <a class="code" href="group__can__general.html#ga01a7a415c95c579750bcdd95a1d245c4">canGetErrorText</a>(stat, buf, <span class="keyword">sizeof</span>(buf));</div><div class="line">    printf(<span class="stringliteral">&quot;%s: failed, stat=%d (%s)\n&quot;</span>, <span class="keywordtype">id</span>, (<span class="keywordtype">int</span>)stat, buf);</div><div class="line">    exit(1);</div><div class="line">  }</div><div class="line">} <span class="comment">// checkStatus</span></div><div class="line"></div><div class="line"><span class="comment">// We check the status returned by the call to canSetBusParams</span></div><div class="line">stat = <a class="code" href="group___c_a_n.html#ga7eb8c2e92cfae57e7ec5031818524301">canSetBusParams</a>(hnd, <a class="code" href="canlib_8h.html#a87f618db13ec4f48610def72cfb40969">canBITRATE_250K</a>, 0, 0, 0, 0, 0);</div><div class="line">checkStatus(<span class="stringliteral">&quot;canSetBusParams&quot;</span>, stat);</div></div><!-- fragment --><h1><a class="anchor" id="section_core_api_calls"></a>
CANlib Core API Calls</h1>
<p>The following calls can be considered the CANlib "core" as they are essential for almost any program that uses the CAN bus.</p><ul>
<li><a class="el" href="group__can__general.html#gaff1ec1d3416d3bdd56336a7b9ac008b1">canInitializeLibrary()</a> see <a class="el" href="page_user_guide_init.html#section_user_guide_init_lib_init">Library Initialization</a></li>
<li><a class="el" href="group___c_a_n.html#gac377d182232fb4ec2fed881c2b9ab300">canOpenChannel()</a> and <a class="el" href="group___c_a_n.html#ga49525373a4d08d93c651ec10f79dd36b">canClose()</a> see <a class="el" href="page_user_guide_chips_channels.html">Open Channel</a></li>
<li><a class="el" href="group___c_a_n.html#ga7eb8c2e92cfae57e7ec5031818524301">canSetBusParams()</a> and <a class="el" href="group___c_a_n.html#gafcd85fbac103dcb123f4cd609be6fa14">canSetBusParamsFd()</a> see <a class="el" href="page_user_guide_chips_channels.html#section_user_guide_init_bit_rate_can">Set CAN Bitrate</a> and <a class="el" href="page_user_guide_chips_channels.html#section_user_guide_init_bit_rate_canfd">Set CAN FD Bitrate</a></li>
<li><a class="el" href="group___c_a_n.html#ga99c7c99cc71580f8099a1407f4f9ea1a">canBusOn()</a> and <a class="el" href="group___c_a_n.html#gaf1786cfbfd542b18b9c599d278837bd9">canBusOff()</a> see <a class="el" href="page_user_guide_send_recv.html#section_user_guide_send_recv_bus_on_off">Bus On / Bus Off</a></li>
<li><a class="el" href="group___c_a_n.html#ga157d49a9343dea68ac953145e02266d8">canRead()</a> and <a class="el" href="group___c_a_n.html#gac01f98e282609b5f6aaf2b1eabfb83ec">canReadWait()</a> see <a class="el" href="page_user_guide_send_recv.html#section_user_guide_send_recv_reading">Reading Messages</a></li>
<li><a class="el" href="group___c_a_n.html#ga62c185329d6741c90102511e2f37983e">canWrite()</a> and <a class="el" href="group___c_a_n.html#ga304cb3a7bc2874c1f8ad361a911bcd5f">canWriteSync()</a> see <a class="el" href="page_user_guide_send_recv.html#section_user_guide_send_recv_sending">Sending Messages</a> </li>
</ul>
</div></div><!-- contents -->
</div><!-- doc-content -->
<!-- HTML footer for doxygen 1.8.13-->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="navelem"><a class="el" href="page_canlib.html">CAN bus API (CANlib)</a></li>
    <li class="footer"> (canlib 5.38) Tue Feb 15 2022</li>
  </ul>
</div>
</body>
</html>
